/*
 * Copyright (c) 2010 Marc Poppleton
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.marcpoppleton.strawberry.provider;

import org.marcpoppleton.strawberry.StrawberryActivity;

import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

public class CoCManager {
    private static String sCocIdSelection;

    private static String[] sArguments1 = new String[1];

    private static final String[] PROJECTION_ID = new String[] { Coc._ID };

    static {
        StringBuilder selection = new StringBuilder();
        selection.append(Coc._ID);
        selection.append("=?");
        sCocIdSelection = selection.toString();
    }

    private CoCManager() {
    }
    
    public static boolean cameraExists(ContentResolver contentResolver, String id) {
        boolean exists;
        Cursor c = null;

        try {
            final String[] arguments1 = sArguments1;
            arguments1[0] = id;
            c = contentResolver.query(Coc.CONTENT_URI, PROJECTION_ID, sCocIdSelection,
                    arguments1, null);
            exists = c.getCount() > 0;
        } finally {
            if (c != null) c.close();
        }

        return exists;
    }

    public static Coc addCamera(ContentResolver resolver, Coc coc) {
    	if (!cameraExists(resolver, coc.getName())){
    		Log.d(StrawberryActivity.LOG,coc.getName() + " is a new contact");
    		final Uri uri = resolver.insert(Coc.CONTENT_URI, coc.getContentValues());
            if (uri != null) {
                return coc;
            }
    	}else{
    		Log.d(StrawberryActivity.LOG,coc.getName()+ " is updated");
            final String[] arguments1 = sArguments1;
            arguments1[0] = coc.getName();
    		int rows = resolver.update(Coc.CONTENT_URI,coc.getContentValues(),sCocIdSelection,arguments1);
    		Log.d(StrawberryActivity.LOG,rows + " rows updated");
    		return findCamera(resolver,coc.getName());
    	}

        return null;
    }
    
    public static Coc findCamera(ContentResolver contentResolver, String id) {
        Coc coc = null;
        Cursor c = null;

        try {
            sArguments1[0] = id;
            c = contentResolver.query(Coc.CONTENT_URI, null, sCocIdSelection,
                    sArguments1, null);
            if (c.getCount() > 0) {
                if (c.moveToFirst()) {
                    coc = Coc.fromCursor(c);
                }
            }
        } finally {
            if (c != null) c.close();
        }

        return coc;
    }    
    
    public static Coc findCamera(ContentResolver contentResolver, Uri data) {
        Coc coc = null;
        Cursor c = null;

        try {
            c = contentResolver.query(data, null, null, null, null);
            if (c.getCount() > 0) {
                if (c.moveToFirst()) {
                    coc = Coc.fromCursor(c);
                }
            }
        } finally {
            if (c != null) c.close();
        }

        return coc;
    }    
    
    public static boolean deleteCamera(ContentResolver contentResolver, String camera) {
        final String[] arguments1 = sArguments1;
        arguments1[0] = camera;
        int count = contentResolver.delete(Coc.CONTENT_URI, sCocIdSelection, arguments1);
        return count > 0;
    }    
}
